package com.dongdong.controller;


import com.dongdong.mapper.TbSellerMapper;
import com.dongdong.model.TbSeller;
import com.dongdong.service.SellerService;
import com.dongdong.utils.PageBean;
import com.dongdong.utils.PageResult;
import com.dongdong.utils.Result;
import com.dongdong.vo.SellerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@RestController
@RequestMapping("/seller")
public class SellerController {

    @Autowired
    private SellerService sellerService;

    @GetMapping("/getSellerByName")
    public TbSeller getSellerByName(@RequestParam String username) {
        if (Objects.equals("qiandu",username)){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }else if (Objects.equals("yijia",username)){
            throw new RuntimeException("被熔断了");
        }
        return sellerService.getSellerByName(username);
    }


    @GetMapping("/getSeller")
    public Result getSeller(HttpServletRequest request){
        SellerVO sellerVO = sellerService.getSeller(request);
        return Result.OK().setData(sellerVO);
    }

    @PutMapping("/updateSeller")
    public Result updateSeller(@RequestBody SellerVO sellerVO){
        sellerService.updateSeller(sellerVO);
        return Result.OK();
    }


    @PutMapping("/updatePwd")
    public Result updatePwd(@RequestParam String oldPwd,@RequestParam String newPwd,HttpServletRequest request){
        return sellerService.updatePwd(oldPwd,newPwd,request);
    }

    @PostMapping("/addSeller")
    public void addSeller(@RequestBody TbSeller seller){
        sellerService.addSeller(seller);
    }

    /**
     * 模糊查询分页商家
     * @param pageBean
     * @return
     */
    @PostMapping("/getSellerPage")
    public Result getSellerPage(@RequestBody PageBean<SellerVO> pageBean){
        PageResult<SellerVO> pageResult = sellerService.getSellerPage(pageBean);
        return Result.OK().setData(pageResult);
    }

    /**
     * 根据用户名查询对象
     * @param sellerId
     * @return
     */
    @GetMapping("/getSellerById")
    public Result getSellerById(@RequestParam String sellerId){
        TbSeller tbSeller = sellerService.getSellerByName(sellerId);
        return Result.OK().setData(tbSeller);
    }

//    修改状态
    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestParam String status,@RequestParam String sellerId){
        sellerService.updateStatus(status,sellerId);
        return Result.OK();
    }
}
